package com.wl156.activities.publish;

import java.io.IOException;

import org.ksoap2.serialization.SoapPrimitive;
import org.xmlpull.v1.XmlPullParserException;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.wl156.MyApp;
import com.wl156.R;
import com.wl156.model.Certificate;
import com.wl156.model.WebServiceClient;
import com.wl156.widget.ProviceSelector;

public class NewSpecialLine extends Activity implements OnClickListener {

	private ProviceSelector mFromProviceSelector;
	private ProviceSelector mToProviceSelector;
	private EditText mName;
	private EditText mDaysDuringJourney;
	private EditText mDaysTruckSchedule;
	private View mFromContactInfos;
	private View mToContactInfos;
	private EditText mPrice1;
	private EditText mPrice2;
	private EditText mStartLocationAddress;
	private EditText mStartLocationContact;
	private EditText mStartLocationMobile;
	private EditText mStartLocationTel;
	private EditText mEndLocationAddress;
	private EditText mEndLocationContact;
	private EditText mEndLocationMobile;
	private EditText mEndLocationTel;

	WebServiceClient mWebServiceClient;
	Certificate mCertificate;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.act_new_special_line);

		mWebServiceClient = new WebServiceClient(this);
		mCertificate = ((MyApp) getApplication()).getCertificateFromXml();

		Button confirm = (Button) findViewById(R.id.confirm);
		confirm.setOnClickListener(this);

		mFromProviceSelector = (ProviceSelector) findViewById(R.id.provice_selector);
		mFromProviceSelector.setTitle("起点: ");
		mFromProviceSelector.setProviceRequried(true);
		mFromProviceSelector.setCityRequried(true);
		mToProviceSelector = (ProviceSelector) findViewById(R.id.to_provice_selector);
		mToProviceSelector.setTitle("终点: ");
		mToProviceSelector.setProviceRequried(true);
		mToProviceSelector.setCityRequried(true);

		mName = (EditText) findViewById(R.id.name);
		mDaysDuringJourney = (EditText) findViewById(R.id.daysDuringJourney);
		mDaysTruckSchedule = (EditText) findViewById(R.id.daysTruckSchedule);
		mFromContactInfos = findViewById(R.id.from_contact_infos);
		mToContactInfos = findViewById(R.id.to_contact_infos);
		mPrice1 = (EditText) findViewById(R.id.price1);
		mPrice2 = (EditText) findViewById(R.id.price2);
		mStartLocationAddress = (EditText) mFromContactInfos
				.findViewById(R.id.locationAddress);
		mStartLocationContact = (EditText) mFromContactInfos
				.findViewById(R.id.locationContact);
		mStartLocationMobile = (EditText) mFromContactInfos
				.findViewById(R.id.locationMobile);
		mStartLocationTel = (EditText) mFromContactInfos
				.findViewById(R.id.locationTel);
		mEndLocationAddress = (EditText) mToContactInfos
				.findViewById(R.id.locationAddress);
		mEndLocationContact = (EditText) mToContactInfos
				.findViewById(R.id.locationContact);
		mEndLocationMobile = (EditText) mToContactInfos
				.findViewById(R.id.locationMobile);
		mEndLocationTel = (EditText) mToContactInfos
				.findViewById(R.id.locationTel);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.confirm:
			final String startLocationLv1 = mFromProviceSelector.getProvice();
			final String startLocationLv2 = mFromProviceSelector.getCity();
			final String startLocationLv3 = mFromProviceSelector.getRegion();
			final String endLocationLv1 = mToProviceSelector.getProvice();
			final String endLocationLv2 = mToProviceSelector.getCity();
			final String endLocationLv3 = mToProviceSelector.getRegion();
			final String daysDuringJourney = mDaysDuringJourney.getText()
					.toString();
			final String daysTruckSchedule = mDaysTruckSchedule.getText()
					.toString();
			final String price1 = mPrice1.getText().toString();
			final String price2 = mPrice2.getText().toString();
			final String lineName = mName.getText().toString();
			final String startLocationAddress = mStartLocationAddress.getText()
					.toString();
			final String startLocationContact = mStartLocationContact.getText()
					.toString();
			final String startLocationMobile = mStartLocationMobile.getText()
					.toString();
			final String startLocationTel = mStartLocationTel.getText()
					.toString();
			final String endLocationAddress = mEndLocationAddress.getText()
					.toString();
			final String endLocationContact = mEndLocationContact.getText()
					.toString();
			final String endLocationMobile = mEndLocationMobile.getText()
					.toString();
			final String endLocationTel = mEndLocationTel.getText().toString();

			if (TextUtils.isEmpty(startLocationLv1)
					|| TextUtils.isEmpty(startLocationLv2)
					|| TextUtils.isEmpty(endLocationLv1)
					|| TextUtils.isEmpty(endLocationLv2)
					|| TextUtils.isEmpty(daysDuringJourney)
					|| TextUtils.isEmpty(daysTruckSchedule)) {
				Toast.makeText(NewSpecialLine.this, "请填写相应字段",
						Toast.LENGTH_LONG).show();
				return;
			}

			new AsyncTask<Void, Void, Object>() {
				ProgressDialog mDialog;

				@Override
				protected void onPostExecute(Object result) {
					if (result instanceof SoapPrimitive) {
						SoapPrimitive sp = (SoapPrimitive) result;
						try {
							Integer.valueOf(sp.toString());
							Toast.makeText(NewSpecialLine.this, "发布成功",
									Toast.LENGTH_LONG).show();
							finish();
						} catch (Exception e) {
							Toast.makeText(NewSpecialLine.this, sp.toString(),
									Toast.LENGTH_LONG).show();
						}
					} else {
						Toast.makeText(NewSpecialLine.this, "发布失败，请稍候重试",
								Toast.LENGTH_LONG).show();
					}
					mDialog.dismiss();
				};

				@Override
				protected void onPreExecute() {
					mDialog = ProgressDialog.show(NewSpecialLine.this, "",
							"发布中");
				};

				@Override
				protected Object doInBackground(Void... params) {
					try {
						return mWebServiceClient.commonPublishSpecialLine(
								mCertificate, startLocationLv1,
								startLocationLv2, startLocationLv3,
								endLocationLv1, endLocationLv2, endLocationLv3,
								daysDuringJourney, daysTruckSchedule, price1,
								price2, lineName, startLocationAddress,
								startLocationContact, startLocationMobile,
								startLocationTel, endLocationAddress,
								endLocationContact, endLocationMobile,
								endLocationTel);
					} catch (IOException e) {
						return e;
					} catch (XmlPullParserException e) {
						return e;
					}
				}
			}.execute();
			break;
		default:
			break;
		}
	}
}
